#include "CAbilityBlast.h"
#include "CMinionNode.h"
#include "CBattle.h"

CAbilityBlast::CAbilityBlast()
{
  // Ability Type
  type = ABL_POINT;

  // The mp cost of the ability
  cost = 15;

  // Range of the ability ( 0 Indicates non targeted spell )
  range = 200;

  // Radius of the AOE ability ( 0 for non AOE spell )
  radius = 100;

  // Duration of the effect
  duration = 0;

  // Delay of the ability
  delay = 3000;


  // Parameter(s) for ability
  param = new f32;

  // Param[0] = damage ratio
  param[0] = 4.0f;
}

CAbilityBlast::~CAbilityBlast()
{
 delete param;

}

bool CAbilityBlast::effect()
{
  // Step 1 : Range Check
  if ( !caster->IsInRange(point, range) )
  {
    std::cout << "Target is Out of Range. " << std::endl;
    //! TODO: Future message
    //state-> getGUI() -> chatListBox -> addItem(L"Target is Out of Range. ");
    return false;
  }

  // Step 2 : Mana Check
  if ( caster->getStats().mp < cost )
  {
    std::cout << "You dont have enough mp to cast this spell." << std::endl;
    return false;
  }
  caster->getStats().mp -= cost;

  // Step 3 : Calculates damage
  s32 ratio = (s32) param[0];
  s32 baseDmg = caster->getStats().mAtk * ratio;// - target->getStats().mDef;


  // Step 4 : AOE Check
  vector <CMinionNode*> vMinion = battle->getMinionList();
  for ( u32 i=0; i<vMinion.size(); i++ )
  {
    CMinionNode *target = vMinion[i];

    if ( target->IsInRange(point, radius) )
    {
      s32 dmg = baseDmg - target->getStats().mDef;

      if ( dmg < 0 )
        dmg = 1;

      target->updateHp(dmg, true);

      // Check for death
      if ( target->getStats().hp == 0 )
      {
        battle->removeMinion(target);
      }
    }
  }

  return true;

}
